/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Design;
using System.Windows.Forms;
using System.ComponentModel;

namespace DaveSexton.DocProject.Sandcastle.BuildComponents.Editors
{
	public abstract class CancelableBaseEditor<TDialog> : UITypeEditor
		where TDialog : CancelableBaseEditorDialog, new()
	{
		#region Public Properties
		#endregion
		
		#region Private / Protected
		#endregion
		
		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="CancelableBaseEditor" /> class for derived classes.
		/// </summary>
		protected CancelableBaseEditor()
		{
		}
		#endregion

		#region Methods
		protected abstract bool Commit(DialogResult result, TDialog dialog, SandcastleBuildComponent component);

		protected abstract void Cancel(TDialog dialog, SandcastleBuildComponent component);

		protected virtual bool ShowDialog(IWin32Window owner, SandcastleBuildComponent component)
		{
			using (TDialog dialog = new TDialog())
			{
				dialog.BuildComponent = component;

				DialogResult result = dialog.ShowDialog(owner);

				if (Commit(result, dialog, component))
					return true;
				else
				{
					Cancel(dialog, component);
					return false;
				}
			}
		}

		[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.LinkDemand, Name = "FullTrust")]
		public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
		{
			return UITypeEditorEditStyle.Modal;
		}

		[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.LinkDemand, Name = "FullTrust")]
		public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
		{
			return ShowDialog(DocProjectEnvironment.MainWindow, (SandcastleBuildComponent) value);
		}
		#endregion
	}
}
